package com.example.ixform.controllers;

import com.example.ixform.services.CommentService;
import com.example.ixform.services.NetizenService;
import com.example.ixform.entities.Comment;
import com.example.ixform.entities.Netizen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("comments")
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    NetizenService netizenService;

    @GetMapping
    public String displayComments(Model model){

        List<Comment> comments= commentService.getAll();
        model.addAttribute("comments",comments);
        return "comments/list-comments";
    }

    @GetMapping("/new")
    public String displayCommentForm(Model model) {
        Comment aComment = new Comment();
        List<Netizen> netizens=netizenService.getAll();
        model.addAttribute("comment", aComment);
        model.addAttribute("allNetizens",netizens);

        return "comments/new-comment";
    }

        @PostMapping("/save")
        public String createComment(Model model,@RequestParam List<Long> netizens, Comment comment){
            commentService.save(comment);
            return "redirect:/comments";
        }

}

